package org.example.managere.service.feibiao.impl;

import org.example.managere.dto.feibiao.AmArchivesOperationDto;
import org.example.managere.entity.feibiao.AmArchivesOperation;
import org.example.managere.repository.feibiao.AmArchivesOperationRepository;
import org.example.managere.service.feibiao.AmArchivesOperationService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 电机运行数据服务实现类
 */
@Service
public class AmArchivesOperationServiceImpl implements AmArchivesOperationService {

    private final AmArchivesOperationRepository operationRepository;

    public AmArchivesOperationServiceImpl(AmArchivesOperationRepository operationRepository) {
        this.operationRepository = operationRepository;
    }

    @Override
    @Transactional
    public AmArchivesOperationDto save(AmArchivesOperationDto operationDto) {
        AmArchivesOperation operation = new AmArchivesOperation();
        BeanUtils.copyProperties(operationDto, operation);
        operation = operationRepository.save(operation);
        return convertToDto(operation);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<AmArchivesOperationDto> findById(Integer id) {
        return operationRepository.findById(id)
                .map(this::convertToDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesOperationDto> findAll() {
        return operationRepository.findAll().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public Page<AmArchivesOperationDto> findAll(Pageable pageable) {
        Page<AmArchivesOperation> operationPage = operationRepository.findAll(pageable);
        List<AmArchivesOperationDto> dtoList = operationPage.getContent().stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, operationPage.getTotalElements());
    }

    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesOperationDto> findByMotorId(String motorId) {
        return operationRepository.findByMotorId(motorId).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<AmArchivesOperationDto> findLatestByMotorId(String motorId) {
        List<AmArchivesOperation> operations = operationRepository.findByMotorIdOrderByOperationTimeDesc(motorId);
        return operations.isEmpty() ? Optional.empty() : Optional.of(convertToDto(operations.get(0)));
    }

    @Override
    @Transactional(readOnly = true)
    public List<AmArchivesOperationDto> findByTemperatureBetween(Double minTemp, Double maxTemp) {
        BigDecimal min = BigDecimal.valueOf(minTemp);
        BigDecimal max = BigDecimal.valueOf(maxTemp);
        return operationRepository.findByTemperatureBetween(min, max).stream()
                .map(this::convertToDto)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        operationRepository.deleteById(id);
    }

    @Override
    @Transactional
    public AmArchivesOperationDto update(AmArchivesOperationDto operationDto) {
        if (!operationRepository.existsById(operationDto.getOperationId())) {
            throw new IllegalArgumentException("无法更新不存在的记录，ID: " + operationDto.getOperationId());
        }
        return save(operationDto);
    }

    private AmArchivesOperationDto convertToDto(AmArchivesOperation operation) {
        AmArchivesOperationDto dto = new AmArchivesOperationDto();
        BeanUtils.copyProperties(operation, dto);
        return dto;
    }
} 